package com.licheng.controller;

import com.licheng.anno.Log;
import com.licheng.pojo.Clazz;
import com.licheng.pojo.ClazzPageQuery;
import com.licheng.pojo.ClazzPageResult;
import com.licheng.pojo.Result;
import com.licheng.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    /**
     * 班级分页查询
     * @param cpq
     * @return
     */

    @GetMapping
    public Result selectClazz(ClazzPageQuery cpq) {
        ClazzPageResult clazzList = clazzService.selectClazz(cpq);
        return Result.success(clazzList);
    }
    /**
     * 添加班级
     * @param clazz 班级信息
     * @return
     */
    @Log
    @PostMapping
    public Result insertClazz(@RequestBody Clazz clazz) {
        if (clazz.getBeginDate().isAfter(clazz.getEndDate())) {
            return Result.error("开课时间应该小于结课时间");
        }
        clazzService.insertClazz(clazz);
        return Result.success();
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id) {
        Result result = clazzService.deleteClazz(id);
        return result;
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Clazz clazz = clazzService.selectById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     * @param clazz
     * @return
     */
    @Log
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/list")
    public Result selectAllStudent() {
        List<Clazz> clazzList = clazzService.selectAllStudent();
        return Result.success(clazzList);
    }
}
